import { faker } from '@faker-js/faker';
import { eventHandler } from 'h3';

const formatterCN = new Intl.DateTimeFormat('zh-CN', {
  timeZone: 'Asia/Shanghai',
  year: 'numeric',
  month: '2-digit',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
});

function generateMockDataList(count: number) {
  const dataList = [];

  for (let i = 1; i <= count; i++) {
    const dataItem: Record<string, any> = {
      oid: i,
      custId: 1001,
      otype: '1',
      ono: 'GROUP-00' + i,
      oname: faker.company.name(),
      pono: null,
      level: 1,
      status: faker.helpers.arrayElement([0, 1]),
      createdTime: formatterCN.format(
        faker.date.between({ from: '2021-01-01', to: '2024-12-31' }),
      ),
      memo: faker.lorem.sentence(),
      lastUpdateBy: 'admin',
    };
    if (faker.datatype.boolean()) {
      dataItem.children = Array.from(
        { length: faker.number.int({ min: 1, max: 5 }) },

        (item, index) => ({
          oid: i * 10 + index,
          custId: 1001,
          otype: '2',
          ono: 'GROUP-00' + i + '-00' + index,
          oname: faker.company.name(),
          pono: dataItem.ono,
          level: 2,
          status: faker.helpers.arrayElement([0, 1]),
          createdTime: formatterCN.format(
            faker.date.between({ from: '2021-01-01', to: '2024-12-31' }),
          ),
          memo: faker.lorem.sentence(),
          lastUpdateBy: 'admin',
        }),
      );
      if (dataItem.children.length > 3) {
        dataItem.children.forEach((item) => {
          item.children = Array.from(
            { length: faker.number.int({ min: 1, max: 5 }) },
            (_, index) => ({
              oid: i * 100 + index,
              custId: 1001,
              otype: '3',
              ono: item.ono + '-00' + index,
              oname: faker.company.name(),
              pono: item.ono,
              level: 3,
              status: faker.helpers.arrayElement([0, 1]),
              createdTime: formatterCN.format(
                faker.date.between({ from: '2021-01-01', to: '2024-12-31' }),
              ),
              memo: faker.lorem.sentence(),
              lastUpdateBy: 'admin',
            }),
          );
        });
      }
    }
    dataList.push(dataItem);
  }

  return dataList;
}

const mockData = generateMockDataList(50);

export default eventHandler(async (event) => {
  const listData = structuredClone(mockData);

  return useResponseSuccess(listData);
  // return usePageResponseSuccess('1', '10', listData);
});
